package com.ruoyi.match.test.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.match.constant.CommonConstants;
import com.ruoyi.match.match.domain.Match;
import com.ruoyi.match.match.mapper.MatchMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.remote.CapabilityType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;

/**
 * @Author sjz
 * @Date 2023/11/21 9:05
 * @Desc
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/match/http")
public class HttpTestController {

    private final MatchMapper historyMatchMapper;

    @SaIgnore
    @GetMapping("/odd")
    public R<?> getStatisticsByJqCount(@RequestParam(value = "matchFlag",required = false) String matchFlag) throws IOException {
        Proxy proxy = null;
        Connection connectProx = Jsoup.connect("http://http.tiqu.letecs.com/getip3?num=1&type=1&pro=&city=0&yys=0&port=1&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4");
        try {
            String text = connectProx.get().body().text();
            if(StrUtil.isNotBlank(text)){
                log.info("代理IP:"+text);
                String[] split = text.split(":");
                proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(split[0], Integer.parseInt(split[1])));
            }
        }catch (Exception e){
            log.error("使用代理发生异常");
            connectProx =  Jsoup.connect("http://http.tiqu.letecs.com/getip3?num=1&type=1&pro=&city=0&yys=0&port=1&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4");
            String text = connectProx.get().body().text();
            if(StrUtil.isNotBlank(text)){
                log.info("代理IP:"+text);
                String[] split = text.split(":");
                proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(split[0], Integer.parseInt(split[1])));
            }
        }

        List<Match> matches = historyMatchMapper.selectList(Wrappers.<Match>lambdaQuery().isNull(Match::getRemark).isNotNull(Match::getMatchDate).last("limit 10"));
        if(CollUtil.isNotEmpty(matches)){
            for (Match match : matches) {
                String matchUrl = String.format(CommonConstants.MATCH_ODDS_CHANGE_URL, match.getMatchFlag(),2);
                Connection connect = Jsoup.connect(matchUrl).proxy(proxy);
                Document document = connect.get();
                Element table = document.selectFirst("#scroller .changeTable");
                assert table != null;
                Elements trList = table.getElementsByTag("tr");
                for (Element tr : trList) {
                    Elements tdList = tr.getElementsByTag("td");
                    Element td1 = tdList.get(0);
                    Elements spanList = td1.getElementsByTag("span");
                    String main = spanList.get(0).text().trim();
                    String level = spanList.get(1).text().trim();
                    String gust = spanList.get(2).text().trim();
                    System.out.printf("main:%s,level:%s,gust:%s%n",main,level,gust);
                    Element td2 = tdList.get(1);
                    String timeAttribute = td2.attr("time").trim();
                    System.out.println("timeAttribute:"+timeAttribute);
                    String timeText = td2.text().trim();
                    System.out.println("timeText:"+timeText);
                }
            }
        }
        return R.ok();
    }
}
